package test3;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;

/**
 * Creat with IntelliJ IDEA
 * Description:
 * User:WZW
 * Date:2022-04-11
 * Time:10:28
 */

class ServerThread extends Thread{
    //用于与客户端进行交流
    Socket socket;
    DataOutputStream out = null;
    DataInputStream in = null;

    public ServerThread(Socket socketAtServer) {
        this.socket = socketAtServer;//获得套接字
        try {
            //由套接字对象获得输入输出流
            out = new DataOutputStream(socket.getOutputStream());
            in = new DataInputStream(socket.getInputStream());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        try {
            out.writeUTF("连接成功！请告知要下载的文件名");
            //读入客户端的消息
            System.out.println("正在等待客户回复……");
            String read = in.readUTF();
            System.out.println("要传输的文件为："+read);
            Boolean bool = false;//用于告知客户端文件是否读取成功的标记
            //查找文件是否存在
            try (InputStream inputStream = new FileInputStream(read)){
                bool = true;//文件读取成功，标记为true
                out.writeBoolean(bool);//告诉客户端文件可传输
                //读取文件
                byte[] flie = new byte[1024];
                int len;
                while (true){
                    len = inputStream.read(flie);
                    if (len == -1){
                        break;//文件读完
                    }
                }
                out.writeUTF("开始传输文件："+read);
                System.out.println("开始传输文件");
                out.write(flie);
                System.out.println("文件传输完毕！");

            } catch (FileNotFoundException fileNotFoundException) {
                String error = "文件不存在！传输失败";
                System.out.println(error);
                //文件读取失败(不存在)，告知客户端传输失败
                out.writeBoolean(bool);
                out.writeUTF(error);
            }
        } catch (IOException e) {//连接异常情况
            System.out.println("连接断开");
        } finally {//关闭连接
            try {
                socket.close();//关闭套接字
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

public class Sever {
    public static void main(String[] args) {
        ServerSocket server = null;//创建一个ServerSocket对象
        Socket socketAtServer = null;
        try {
            server = new ServerSocket(9870);//server在9870端口监听客户请求
            System.out.println("服务器启动完毕");
        } catch (IOException e) {
            System.out.println("服务器启动出错……请重新启动");
        }
        while (true){
            try {
                socketAtServer = server.accept();//使用accept()来接收客户端的请求，并生成一个Socket对象
                System.out.println("来自:" + socketAtServer.getInetAddress() + "的客户端");
            } catch (IOException e) {
                System.out.println("服务器正在监听……");
            }

            if (socketAtServer != null) {
                ServerThread serverThread = new ServerThread(socketAtServer);
                serverThread.start(); //为每个客户端启动一个专门的线程
            } else {
                continue;
            }
        }

    }

}